package com.software.myopiachildren.ui.login;


import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.software.myopiachildren.R;
import com.software.myopiachildren.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 导航页
 *
 * @author penglei
 * @date 2020/05/15
 */
public class GuideActivity extends BaseActivity {
    private static final String TAG = "GuideActivity";

    @BindView(R.id.viewPager_welcome)
    ViewPager mViewPagerWelcome;
    @BindView(R.id.layout_container)
    LinearLayout mLayoutContainer;

    private List<View> list = null;
    private int[] imgIds = new int[]{R.drawable.ic_guide1, R.drawable.ic_guide2, R.drawable.ic_guide3};
    private ImageView[] dots;
    private Button imageButton_viewpager_submit;

    @Override
    protected int getContentViewId() {
        // 全屏、隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_guide;
    }

    @Override
    public void initView() {
        list = new ArrayList<View>();
        for (int i = 0; i < imgIds.length - 1; i++) {
            ImageView imgObj = new ImageView(this);
            imgObj.setImageResource(imgIds[i]);
            list.add(imgObj);
        }

        View lastPagerView = getLayoutInflater().inflate(R.layout.item_viewpager_last, null);
        list.add(lastPagerView);

        mViewPagerWelcome.setAdapter(new MyAdapter(list));
        mViewPagerWelcome.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < imgIds.length; i++) {
                    dots[i].setEnabled(true);
                }
                dots[position].setEnabled(false);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {


            }

            @Override
            public void onPageScrollStateChanged(int arg0) {


            }
        });
        initDots();
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initDots() {
        dots = new ImageView[imgIds.length];
        LinearLayout layout = (LinearLayout) findViewById(R.id.layout_container);
        for (int i = 0; i < imgIds.length; i++) {
            dots[i] = (ImageView) layout.getChildAt(i);
            dots[i].setEnabled(true);
            dots[i].setTag(i);
            dots[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewPagerWelcome.setCurrentItem((Integer) v.getTag());
                }
            });
        }
        dots[0].setEnabled(false);
    }

    class MyAdapter extends PagerAdapter {
        private List<View> list = null;

        public MyAdapter(List<View> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
        }
    }

    public void clickButton(View view) {
        switch (view.getId()) {
            case R.id.imageButton_viewpager_submit:
                Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.main_into_the, R.anim.main_out_the);
                finish();
                break;

            default:
                break;
        }
    }
}
